<script src="../../main.js"></script>
<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-picture-outline"></i> 话题管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                  <el-button
                        type="primary"
                        icon="el-icon-lx-roundadd"
                        class="handle-del mr10"
                        @click="addItem"
                        v-if="globalPermission.topic.edit"
                   >新增话题
                    </el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column prop="id" label="话题id"></el-table-column>
                <el-table-column prop="topicName" label="话题名称"></el-table-column>
                <el-table-column prop="typeName" label="所属分类"></el-table-column>
                <el-table-column label="推荐">
                    <template slot-scope="scope">
                        <el-tag
                                :type="scope.row.recommend==0?'success':(scope.row.recommend===1?'danger':'')"
                        >{{scope.row.recommend==0?"不推荐":"推荐"}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="createdTime" label="创建时间"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.row.id,scope.row)"
                                v-if="globalPermission.topic.edit"
                        >查看编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageNum"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="查看/编辑" :visible.sync="editVisible" width="40%">
                <el-form ref="form" :model="form" label-width="170px">
                         <el-form-item label="所属分类">
                            <el-select v-model="form.typeId" placeholder="分类名称" class="handle-input mr10">
                                <el-option
                                        v-for="item in colOptions"
                                        :key="item.key"
                                        :label="item.label"
                                        :value="item.key">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="话题名称" >
                            <el-input v-model="form.topicName"></el-input>
                        </el-form-item>
                        <el-form-item label="是否推荐">
                            <el-radio-group v-model="form.recommend">
                                <el-radio :label="1">是</el-radio>
                                <el-radio :label="0">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="帖子封面">
                            <span  class="el-upload_tip" style="color:red;font-size:12px">点击下面图片即可观看大图</span>
                        </el-form-item>
                        <el-image
                                class="el-cover"
                                :src="form.coverUrl"
                                :preview-src-list="coverUrls"
                        ></el-image>
                        <el-upload
                               ref="uploadPicture"
                               class="el-upload"
                               action="http://localhost:8098/manager/topic/uploadImg"
                               :headers="getToken"
                               :on-success="refreshPic"
                               multiple
                        >
                            <el-button size="medium" type="primary" v-if="globalPermission.topic.edit">上传文件</el-button>
                        </el-upload>
                </el-form>

                 <span slot="footer" class="dialog-footer">
                    <el-button @click="clean">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                 </span>
            </el-dialog>
    </div>
</template>

<script>
    import {getAll,editCol,delBanner,saveBanner,getDetail,getAllTabs} from '../../api/topic';
    var queryRequest = {url: "/manager/topic/getTopicList"};
    var editRequest = {url:"/manager/topic/updateOrSaveTopic"};
    var delRequest = {url:"/manager/topic/deleteTopicType"};
    var getDetailRequest = {url:"/manager/topic/getTopicInfo"};
    var queryTabsRequest = {url: "/manager/topic/getAll"};
    export default {
        name: 'usertable',
        data() {
            return {
                query: {
                    title: '',
                    pageNum: 1,
                    pageSize: 10,
                },
                statusQuery:{
                    id:'',
                    status
                },
                coverUrls :[],
                picData :[],
                globalPermission: this.getPermission(),
                colOptions:this.getColOptions(),
                getToken:{'auth-token':localStorage.getItem('token')},
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                editRolesVisible: false,
                formIdShow: false,
                pageTotal: 0,
                form: {},
                statusForm:{},
                idx: -1,
                id: -1,
                isCurEdit:false,
            };
        },
        created() {
            queryRequest.data = this.query;
            this.getData();
        },
        methods: {
            // 获取数据
            getData() {
                //获取新闻列表
                getAll(queryRequest).then(res => {
                    this.tableData = res.data.list;
                    this.pageTotal = res.data.total;
                });
            },
            getColOptions(){
                const data = [];
                queryTabsRequest.data ={
                        colName: '',
                        positionName: '',
                        pageNum: 1,
                        pageSize: 20
                };
                getAllTabs(queryTabsRequest).then(res =>{
                    if(res.data.list!=null){
                      res.data.list.forEach(function (tabs) {
                            data.push({
                                label: tabs.typeName,
                                key: tabs.id
                            });
                        });
                    }
                });
                return data;
            },
            clean(){
                this.editVisible=false;
                this.handleSearch();
            },
            getPermission() {
                return JSON.parse(sessionStorage.getItem("permission"));
            },

            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageNum', 1);
                this.getData();
            },
            // 编辑操作
            handleEdit(id, row) {
               this.id = id;
               getDetailRequest.id =id;
               getDetail(getDetailRequest).then(res=>{
                  this.form = res.data;
                  var imgs = res.data.coverUrl;
                  if(imgs!=null){
                     this.coverUrls = imgs.split(',');
                  }
               });
               this.editVisible = true;
               this.isCurEdit = true;
            },
            refreshPic(response){
                this.$message.success('图片上传成功');
                this.form.coverUrl = response.data;
                this.coverUrls = response.data.split(',');
                this.handleSearch();
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            // 新增
            addItem() {
                this.form={};
                this.isCurEdit = false;
                this.editVisible = true;
            },

            // 保存编辑
            saveEdit() {
                editRequest.data = this.form;
                editCol(editRequest).then(res => {
                    if (res.code == 0) {
                        this.$message.success(`成功`);
                        this.editVisible = false;
                        this.handleSearch();
                    } else {
                        this.$message.error(res.message);
                    }
                });
            },
            // 删除操作
            handleDelete(id, row) {
                // 二次确认删除
                this.$confirm("你确定要删除吗", '提示', {
                    type: 'warning'
                }).then(() => {
                    this.form.id = id;
                    delRequest.data = this.form;
                    editCol(delRequest).then(res => {
                        if (res.code == 0) {
                            this.$message.success(`修改成功`);
                            this.editVisible = false;
                            this.handleSearch();
                        } else {
                            this.$message.error(res.message);
                        }
                    });
                    //this.tableData.splice(index, 1);
                }).catch(() => {
                });
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageNum', val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 130px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }

    .show {
        display: block;
    }

    .hide {
        display: none;
    }

    .checkbox-rows {
        width: 130px;
        margin-top: 10px;
    }

    .el-checkbox.is-bordered + .el-checkbox.is-bordered {
        margin-left: 0px;
    }
    .el-upload{
        background-color: #fff;
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        height: 40px;
        text-align: center;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .el-cover{
        display: block;
        margin: auto;
        width: 200px;
        height: 150px;
    }

</style>
